package com.bus.stream

import java.sql.{Connection, PreparedStatement}
import java.text.SimpleDateFormat
import java.util.Date

object Dao {

  def update(con: Connection, bus: String, num: Float): Unit = {
    var state: PreparedStatement = null
    try {
      con.setAutoCommit(false)
      val sql = "update t_s_bus_num set num=? where bus=?"
      state = con.prepareStatement(sql)

      state.setFloat(1, num)
      state.setString(2, bus)
      state.addBatch()

      state.executeBatch()
      con.commit()
    } catch {
      case e: Exception => e.printStackTrace()
    } finally {
      if (state != null) {
        state.close()
      }
    }
  }

  def insert(con: Connection, station: String, num: Float): Unit = {
    var state: PreparedStatement = null
    try {
      con.setAutoCommit(false)
      val sql = "insert into t_s_station_num values(?,?)"
      state = con.prepareStatement(sql)

      state.setString(1, station)
      state.setFloat(2, num)
      state.addBatch()

      state.executeBatch()
      con.commit()
    } catch {
      case e: Exception => e.printStackTrace()
    } finally {
      if (state != null) {
        state.close()
      }
    }
  }

  def delete(): Unit = {
    var con: Connection = null
    try {
      con = MySQLUtil.getConnection()
      con.setAutoCommit(false)
      val sql = "delete from t_s_station_num"
      con.prepareStatement(sql).executeUpdate()
      con.commit()
    } catch {
      case e: Exception => e.printStackTrace()
    } finally {
      MySQLUtil.release(con, null)
    }
  }

}
